﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

using Newtonsoft.Json;

using Coding4Fun.Lockscreen.Core;
using Coding4Fun.Lockscreen.Web.Models;

namespace Coding4Fun.Lockscreen.Web.DataStore
{
    public class DataEngine
    {
        private readonly HttpClient _client;

        public DataEngine()
        {
            _client = new HttpClient();
            _client.DefaultRequestHeaders.Add("X-ZUMO-APPLICATION", AuthConstants.AmsApiKey);
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        public IEnumerable<Category> GetAllCategories()
        {
            var result = _client.GetStringAsync(string.Concat(AuthConstants.AmsTableUrl,"Category")).Result;
            IEnumerable<Category> categories = JsonConvert.DeserializeObject<IEnumerable<Category>>(result);
            return categories;
        }

        public IEnumerable<Image> GetAllImages()
        {
            var result = _client.GetStringAsync(string.Concat(AuthConstants.AmsTableUrl, "Image")).Result;
            IEnumerable<Image> images = JsonConvert.DeserializeObject<IEnumerable<Image>>(result);
            return images;
        }

        public Category GetCategoryById(int id)
        {
            string composite = string.Concat(AuthConstants.AmsTableUrl, "Category?$filter=(id%20eq%20", id.ToString(), ")");
            var result = _client.GetStringAsync(composite).Result;
            IEnumerable<Category> categories = JsonConvert.DeserializeObject<IEnumerable<Category>>(result);
            return categories.FirstOrDefault();
        }

        public Image GetImageById(int id)
        {
            string composite = string.Concat(AuthConstants.AmsTableUrl, "Image?$filter=(id%20eq%20", id.ToString(), ")");
            var result = _client.GetStringAsync(composite).Result;
            IEnumerable<Image> images = JsonConvert.DeserializeObject<IEnumerable<Image>>(result);
            return images.FirstOrDefault();
        }

        public IEnumerable<Image> GetImagesByCategoryId(int id)
        {
            string composite = string.Concat(AuthConstants.AmsTableUrl, "Image?$filter=(CategoryID%20eq%20", id.ToString(), ")");
            var result = _client.GetStringAsync(composite).Result;
            IEnumerable<Image> images = JsonConvert.DeserializeObject<IEnumerable<Image>>(result);
            return images;
        }

        public HttpStatusCode AddCategory(Category category)
        {
            var serializedObject = JsonConvert.SerializeObject(category, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
            var request = new HttpRequestMessage(HttpMethod.Post, string.Concat(AuthConstants.AmsTableUrl, "Category"));
            request.Content = new StringContent(serializedObject, Encoding.UTF8, "application/json");

            var response = _client.SendAsync(request).Result;

            return response.StatusCode;
        }

        public HttpStatusCode UpdateCategory(Category category)
        {
            var request = new HttpRequestMessage(new HttpMethod("PATCH"), string.Concat(AuthConstants.AmsTableUrl, "Category", "/", category.id));
            var serializedObject = JsonConvert.SerializeObject(category);

            request.Content = new StringContent(serializedObject, Encoding.UTF8, "application/json");

            var response = _client.SendAsync(request).Result;
            return response.StatusCode;
        }

        public HttpStatusCode DeleteCategoryFromId(int categoryId)
        {
            var request = new HttpRequestMessage(HttpMethod.Delete, string.Concat(AuthConstants.AmsTableUrl, "Category", "/", categoryId));

            var response = _client.SendAsync(request).Result;
            return response.StatusCode;
        }

        public HttpStatusCode AddImage(Image image)
        {
            var serializedObject = JsonConvert.SerializeObject(image, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
            var request = new HttpRequestMessage(HttpMethod.Post, string.Concat(AuthConstants.AmsTableUrl, "Image"));
            request.Content = new StringContent(serializedObject, Encoding.UTF8, "application/json");

            var response = _client.SendAsync(request).Result;

            return response.StatusCode;
        }

        public HttpStatusCode UpdateImage(Image image)
        {
            var request = new HttpRequestMessage(new HttpMethod("PATCH"), string.Concat(AuthConstants.AmsTableUrl, "Image", "/", image.id));
            var serializedObject = JsonConvert.SerializeObject(image);

            request.Content = new StringContent(serializedObject, Encoding.UTF8, "application/json");

            var response = _client.SendAsync(request).Result;
            return response.StatusCode;
        }

        public HttpStatusCode DeleteImageFromId(int imageId)
        {
            var request = new HttpRequestMessage(HttpMethod.Delete, string.Concat(AuthConstants.AmsTableUrl, "Image", "/", imageId));

            var response = _client.SendAsync(request).Result;
            return response.StatusCode;
        }
    }
}